#!/bin/sh

# Uncomment the next row to debug the script
#set -x


###############################################################################
#                             
###############################################################################

name=`basename $0`
currdir=`pwd`
extension=".java"
runlocal=0
skip_comments=0
use_default_header=0
headerfile=0

awk_filter="${currdir}/addheader.awk"
srcdir="${currdir}/../src"

###############################################################################
#                                 F u n c t i o n s
###############################################################################

echoHelp() {

echo " 

This program acts recursively on all files with extension '.java' replacing
the heading (from beginning lines up to the word 'package') with the heading
specified with <headerfile>. 

WARNING: you is advised strongly to execute the program on a copy of
         the surce files.

Usage: 
$name [-help] 
$name [-local] [-print] -def
$name [-local] [-print] headerfile

Where
    -help      view this help
    -print     print on stdout the default header.
    -def       use default header.
    -local     run only on files in the current directory.
    
     <headerfile> the header file.
"

}

writeDefaultHeader() {
	echo "/*---------------------------------------------------------------------------*/" >  $1
	echo "/*                                                                           */" >> $1
	echo "/* Copyright 20014-2015 Davide Bettoni (https://www.linkedin.com/in/dbettoni)*/" >> $1
	echo "/*                                                                           */" >> $1
	echo "/*---------------------------------------------------------------------------*/" >>  $1
	echo "" >> $1
}


###############################################################################
#                                 M a i n
###############################################################################

if [ $# -ge 1 ] && [ $# -le 4 ] ; then

	while [ $# -gt 0 ]; do
		
		case $1 in
			
			-help)		echoHelp; exit 1 ;;
            
            -print)     writeDefaultHeader /tmp/_def_.$$;
						cat /tmp/_def_.$$; rm -f /tmp/_def_.$$;
						exit 0;;

			-local) 	runlocal=1 ;;
	
			-skip)	    skip_comments=1 ;;
							 			
            -def)       use_default_header=1;;

			-*)			echo "$1 invalid parameter!";
	 					exit 1 ;;
	
			*)	  		if [ $use_default_header = 0 ]; then 
							headerfile=$1
							if [ -r $headerfile ]; then
								break;
							else
								echo "$headerfile file not found."; exit 1 ;
							fi
						fi;;
		esac

		shift;
	
	done

else
	echoHelp 
fi


if [ $headerfile = 0 ] && [ $use_default_header = 0 ]; then
	echo "you have to specify a header file or to use the option -def"
	exit 1;
fi

if [ $use_default_header = 1 ]; then
	#write default header on tmpfile
	headerfile=/tmp/default.$$
	writeDefaultHeader $headerfile
fi

if [ $runlocal = 1 ]; then
	files=*${extension}
else
	files=`find ${srcdir} -name "*${extension}"`	
fi



#filter all files
for file in $files ; do

	echo "Process file: $file"

	tmpfile=`basename $file`
	tmpfile=/tmp/${tmpfile}.$$

	awk -f ${awk_filter} $headerfile $file > $tmpfile
	#sostituisco il file
	mv -f $tmpfile $file

done

if [ $use_default_header = 1 ]; then
	rm -f $headerfile
fi

